"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.removeUnit = exports.numberFormat = void 0;
/**
 * 数值类型字符串千分位处理
 * @param string 数值类型字符串
 * @param decimalPlaces 小数位数，范围0-20，
 * 0为无小数位数并进行四舍五入，若该参数不传为与原数保持一致
 * @return {string}
 */
const numberFormat = (str, options) => {
  let decimalPlaces = options?.decimalPlaces;
  const navigator = window?.navigator;
  const isSupported = Boolean(navigator && 'language' in navigator);
  if (!isSupported) throw new Error('The environment does not support transformation');
  let fractionDigits = 0;
  if (str.indexOf('.') !== -1) {
    fractionDigits = str.split('').slice(str.indexOf('.') + 1, str.split('').length).length;
  }
  if (typeof decimalPlaces !== 'number' || decimalPlaces < 0 || decimalPlaces > 20) decimalPlaces = undefined;
  return new Intl.NumberFormat(navigator?.language ?? 'zh-CN', {
    minimumFractionDigits: decimalPlaces ?? fractionDigits,
    maximumFractionDigits: decimalPlaces ?? fractionDigits
  }).format(Number(str));
};

/**
 * 数值类型字符串去除单位
 * @param string 数值类型字符串
 * @return {number}
 */
exports.numberFormat = numberFormat;
const removeUnit = (str, isExtracted = false) => {
  // const reg = /^[\d]+[.\da-zA-Z]*[a-zA-Z\u4e00-\u9fa5]$/;
  // if (!reg.test(str)) throw;
  return isExtracted ? {
    value: Number(str.replace(/[\u4e00-\u9fa5a-zA-Z]/g, '')),
    unit: str.replace(/[^a-zA-Z\u4e00-\u9fa5]/g, '')
  } : Number(str.replace(/[\u4e00-\u9fa5a-zA-Z]/g, ''));
};
exports.removeUnit = removeUnit;